package com.devilvan.controller;


import com.devilvan.entity.Dept;
import com.devilvan.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2020-11-10
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping(value = "queryDeptById/{dept_id}" ,produces = "application/json; charset=UTF-8")
    @HystrixCommand(fallbackMethod = "hystrixQueryDeptById")
    public Dept queryDeptById(@PathVariable("dept_id") Long dept_id) {
        Dept dept = deptService.queryDeptById(dept_id);

        if (dept == null){
            throw new RuntimeException("该ID：" + dept_id + "不存在，请输入正确的ID");
        }

        return dept;
    }
    // 给该服务加上熔断机制

    // 备选方案Hystrix
    public Dept hystrixQueryDeptById(@PathVariable("dept_id") Long dept_id) {
        return new Dept()
                .setDeptId(dept_id)
                .setDeptName("该ID：" + dept_id + "不存在，请输入正确的ID  --@Hystrix")
                .setDbSource("没有这个数据库  --@Hystrix");
    }
}
